package com.F09.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.F09.actionform.F0904ActionForm;
import com.F09.dao.OrgUserDAO;
import com.action.BaseAction;
import com.config.Constants;
import com.info.UserInfo;
import com.util.CommonTools;
import com.util.SessionUtils;

/**
 * �û��޸��������
 * @file F0904Action.java
 * @author Liushimin
 * @since 2008-10-14
 * @version 1.0
 */
public class F0904Action extends BaseAction {

	/**
	 * Method execute
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		F0904ActionForm actionForm = (F0904ActionForm) form;
		OrgUserDAO dao = new OrgUserDAO();
		UserInfo info = new UserInfo();
		String operationType = actionForm.getOperationType();
		String userId = SessionUtils.getUserId(request);
		String managOrgName = SessionUtils.getManagOrgName(request);
		String userName = SessionUtils.getUserName(request);
		String orgName = SessionUtils.getOrgName(request);
		String strSubmit = "submit";
		String strDetail = "detail";
		if(CommonTools.isNullString(actionForm.getSearchState())){
			actionForm.setSearchState(Constants.STATE_01);
		}
		try {
			if(strSubmit.equals(operationType)){
				if(!actionForm.getNEWPASSWORD().equals(actionForm.getNEWPASSWORD1())){
					actionForm.setErrorFlag("两次密码输入不一致！�£�");
					actionForm.setNEWPASSWORD("");
					actionForm.setNEWPASSWORD1("");	
					return mapping.findForward("queryList");
				}
				info.setTel(actionForm.getTEL());				
				info.setMobil(actionForm.getMOBIL());		
				info.setNotes(actionForm.getNOTES());
				info.setPassword(actionForm.getNEWPASSWORD());	
				info.setUserId(userId);
				info.setOldpassword(actionForm.getOLDPASSWORD());
				if("".equals(actionForm.getNEWPASSWORD())||actionForm.getNEWPASSWORD()==null){
					if(dao.updateUserInfo(info)){
						actionForm.setErrorFlag("用户信息修改成功!");
					}else{
						actionForm.setErrorFlag("��ݿⷢ���数据库发生异常，请与管理员联系！");
					}
				}else{
					String temp = dao.updatePassword(info);
					if(Constants.NO.equals(temp)){
						actionForm.setErrorFlag("�û�ԭ���用户原密码不正确!");
					}else if(Constants.YES.equals(temp)){
						actionForm.setErrorFlag("用户信息修改成功！");
					}else{
						actionForm.setErrorFlag("数据库发生异常，请与管理员联系！");
					}
				}
				actionForm.setOLDPASSWORD("");
				actionForm.setNEWPASSWORD("");
				actionForm.setNEWPASSWORD1("");	
			}

			if(Constants.INIT.equals(operationType)){
				actionForm.setMANAGORGNAME(managOrgName);
				actionForm.setUSERNAME(userName);
				actionForm.setORGNAME(orgName);
				actionForm.setOLDPASSWORD("");
				actionForm.setNEWPASSWORD("");
				actionForm.setNEWPASSWORD1("");
				Map map = null;
				List list = new ArrayList();
				list = dao.selectByPassword(userId);
				 for(int i = 0; i < list.size();i++){
					map = (Map)list.get(i);	
					actionForm.setPASSWORD(CommonTools.null2String((String)map.get("PASSWORD")));		
					actionForm.setTEL(CommonTools.null2String((String)map.get("TEL")));				
					actionForm.setMOBIL(CommonTools.null2String((String)map.get("MOBIL")));			
					actionForm.setNOTES(CommonTools.null2String((String)map.get("NOTES")));
				 }
				return mapping.findForward(strDetail);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (dao != null) dao.disConnect();
		}
		return mapping.findForward(strDetail);
	}
}
